import Vue from 'vue'
import Vuex from 'vuex'

import {
  tbDeptQuery,
  tbDeptAdd,
  tbDeptUpdate,
  tbDeptDelete,
} from '@/api/admin/user/tbSysDept'

Vue.use(Vuex)

const state = {
  deptList: [],
}

const getters = {}

const actions = {
  async queryTbDeptList({ commit }, listQuery) {
    let res = await tbDeptQuery({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async deleteTbDept({ commit }, row) {
    let res = await tbDeptDelete(row.id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async addTbDept({ commit }, data) {
    let res = await tbDeptAdd(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async updateTbDept({ commit }, data) {
    let res = await tbDeptUpdate(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
}

const mutations = {
  SET_Dept: (state, d) => {
    state.deptList = d
  },
}

export default {
  state,
  getters,
  actions,
  mutations,
}
